<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/axios.min.js"></script>
  </head>
  <body>
    <ul>
      <!-- <li>
        <h2></h2>
        <p></p>
        <img src="" alt="">
      </li> -->
    </ul>

    <script>
      /*   所有商品接口
            get
            参数
            pagesize  每页获取几个数据
            pagenum 获取第几页数据
            uid  用户id
            http://jx.xuzhixiang.top/ap/api/allproductlist.php?pagesize=20&pagenum=4 */

      let pagesize = 10;
      let pagenum = 0;
      // 用户id
      let id = localStorage.getItem("id");
      let token = localStorage.getItem("token");
      laodList();
      // 封装加载商品列表的函数 --- 页面一开始需要 加载商品列表-- 发布了商品列表得更新 加载商品列表
      function laodList() {
        let listAPI = "http://jx.xuzhixiang.top/ap/api/allproductlist.php";
        axios
          .get(listAPI, {
            params: {
              pagesize,
              pagenum,
              uid: id,
            },
          })
          .then((r) => {
            console.log(r.data);
            //商品数组
            let productArr = r.data.data;
            // {pname:'aa',pprice:'111'}{}
            // console.log();

            // 根据商品数组- 拼接字符串 给ul
            let str = "";
            productArr.forEach((pObj) => {
              str += `<li>
                        <a href="item.html?id=${pObj.pid}">
                          <h2>${pObj.pname}</h2>
                          <p>${pObj.pprice}</p>
                          <p>${pObj.pid}</p>
                          <img src="${pObj.pimg}" alt="">
                        </a>
                      </li>`;
            });

            document.querySelector("ul").innerHTML = str;
          });
      }
    </script>
  </body>
</html>
